<template>
  <div>
    <div class="search-term">
      <el-form>
        <el-form-item>
          <el-button @click="createNginxPNode()" type="primary">增加集群POOL节点</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table
            :data="tableData"
            border
            ref="multipleTable"
            stripe
            style="width: 100%"
            tooltip-effect="dark"
    >
      <el-table-column label="节点名" prop="node_name" width="120"></el-table-column>
      <el-table-column label="地址" prop="ip" ></el-table-column>
      <el-table-column label="端口" prop="port" ></el-table-column>
      <el-table-column label="权重" prop="weight" ></el-table-column>
      <el-table-column label="最大失败次数" prop="max_failed" ></el-table-column>
      <el-table-column label="超时" prop="timeout" ></el-table-column>
      <el-table-column label="按钮组" width="220" >
        <template slot-scope="scope">
          <el-button @click="updateNginxPNode(scope.row)" icon="el-icon-edit" size="small" type="primary">编辑</el-button>
          <el-button @click="deleteNginxPNode(scope.row)" type="danger" icon="el-icon-delete" style="margin-left: 6px;" size="mini" slot="reference">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
            :current-page="page"
            :page-size="pageSize"
            :page-sizes="[10, 30, 50, 100]"
            :style="{float:'right',padding:'20px'}"
            :total="total"
            @current-change="handleCurrentChange"
            @size-change="handleSizeChange"
            layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>

    <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible" width="600px" >
      <el-form ref="elForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
        <el-form-item label="节点名:" prop="node_name">
          <el-input v-model="formData.node_name" clearable placeholder="请输入" ></el-input>
        </el-form-item>
        <el-form-item label="IP:" prop="ip">
          <el-input v-model="formData.ip" clearable placeholder="请输入" ></el-input>
        </el-form-item>
        <el-form-item label="端口:" prop="port">
          <el-input v-model="formData.port" clearable placeholder="请输入" ></el-input>
        </el-form-item>
        <el-form-item label="权重:" prop="weight">
          <el-input v-model="formData.weight" clearable placeholder="请输入" ></el-input>
        </el-form-item>
        <el-form-item label="最大失败次数:" prop="max_failed">
          <el-input v-model="formData.max_failed" clearable placeholder="请输入" ></el-input>
        </el-form-item>
        <el-form-item label="超时时间:" prop="timeout">
          <el-input v-model="formData.timeout" clearable placeholder="请输入" ></el-input>
        </el-form-item>
        <el-form-item label="状态控制:">
          <el-radio-group v-model="formData.status">
            <el-radio  label="Enable" >Enable</el-radio>
            <el-radio  label="Disable">Disable</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>

      <div class="dialog-footer" slot="footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button @click="enterDialog" type="primary">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    createNginxPNode,
    updateNginxPNode,
    deleteNginxPNode,
    getNginxPNodeList
  } from "@/api/nginx_pool_node"; //  此处请自行替换地址
  import { formatTimeToStr } from "@/utils/date";
  import infoList from "@/mixins/infoList";

  export default {
    name: "poolNode",
    mixins: [infoList],
    data() {
      return {
        listApi: getNginxPNodeList,
        dialogFormVisible: false,
        dialogTitle: "增加POOL节点",
        visible: false,
        type: "",
        searchInfo: {
          cluster_id: 0,
        },
        formData: {
          cluster_id: 0,
          pool_name: '',
          node_name: '',
          ip: '',   // 库号
          port: '',  // 字典标签值
          weight: '1',  // 字典标签值
          max_failed: '3',
          timeout: '3',
        },
        rules: {
          node_name: [{required: true, message: "节点名字", trigger: "blur"}],
          ip: [{required: true, pattern: /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/, message: "IP地址", trigger: "blur"}],
          port: [{required: true, pattern:/^[0-9]*$/, message: "端口", trigger: "blur"}],
          weight: [{pattern:/^[0-9]*$/, message: "权重", trigger: "blur"}],
          timeout: [{pattern:/^[0-9]*$/, message: "超时时间", trigger: "blur"}],
          max_failed: [{pattern:/^[0-9]*$/, message: "最大失败次数", trigger: "blur"}]
        }
      };
    },
    filters: {
      formatDate: function(time) {
        if (time != null && time != "") {
          var date = new Date(time);
          return formatTimeToStr(date, "yyyy-MM-dd hh:mm:ss");
        } else {
          return "";
        }
      },
    },
    methods: {
      //条件搜索前端看此方法
      handleQuery() {
        this.getTableData();

      },
      async updateNginxPNode(row) {
        this.type = "update";
        this.dialogTitle = '编辑'
        this.formData = row
        // this.formData.pool_name = row.pool.pool_name
        this.formData.pool_name = this.$route.query.pool_name
        this.dialogFormVisible = true;
      },
      closeDialog() {
        this.dialogFormVisible = false;
        this.formData = {
          node_name:"",
          ip:"",
          port:"",
          weight:"1",
          max_failed:"2",
          timeout:"3",
          status:"Enable",
          cluster_id: parseInt(this.$route.query.cluster_id)
        };
        this.searchInfo.cluster_id = parseInt(this.$route.query.cluster_id)
        this.formData.pool_name = this.$route.query.pool_name
      },
      deleteNginxPNode(row) {
        this.$confirm("此操作将永久删除库记录, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(async () => {
            this.visible = false;
            const res = await deleteNginxPNode({ ID: row.ID });
            if (res.code == 0) {
              this.$message({
                type: "success",
                message: "删除成功!"
              });
              await this.getTableData();
            }
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除"
            });
          });
      },
      async enterDialog() {
        this.$refs['elForm'].validate(async valid => {
          if (!valid) return
          let res;
          switch (this.type) {
            case "create":
              this.formData.id = null
              res = await createNginxPNode(this.formData);
              break;
            case "update":
              res = await updateNginxPNode(this.formData);
              break;
            default:
              res = await createNginxPNode(this.formData);
              break;
          }
          if (res.code == 0) {
            this.$message({
              type:"success",
              message:"创建/更改成功"
            })
            this.closeDialog();
            await this.getTableData();
          }
        })

      },
      async createNginxPNode(){
        this.dialogTitle = '增加库号记录'
        this.type = 'create';
        this.dialogFormVisible = true;
      }
    },
    async created() {
      this.searchInfo.cluster_id = parseInt(this.$route.query.cluster_id)
      this.formData.cluster_id = parseInt(this.$route.query.cluster_id)
      this.formData.pool_name = this.$route.query.pool_name
      await this.getTableData();

    }
  };
</script>

<style>
</style>
